package cn.zjutleo.village_server.Test;

import cn.zjutleo.village_server.dto.PolicyDto;
import cn.zjutleo.village_server.dto.PolicySelectDto;
import cn.zjutleo.village_server.vo.PolicyVo;
import com.alibaba.fastjson.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import javax.ws.rs.core.MediaType;

import java.text.DateFormat;

import static org.apache.coyote.http11.Constants.Z;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

/**
 * PolicyControllerTest
 *
 * @author CMT
 * @since
 */
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class PolicyControllerTest {
    @Autowired
    private MockMvc mockMvc;
    @Before
    public void setUp() throws Exception {
        System.out.println("---------------start---------------");
        System.out.println("================end================");
    }

    @Test
    public void addPolicyTest() throws Exception {

        PolicyVo policyVo = new PolicyVo( ) ;
        policyVo.setPolicyId(103);
        policyVo.setPolicyAuther("string");
        policyVo.setPolicyBriefintroduction("string");
        policyVo.setPolicyColletCount(0);
        policyVo.setPolicyDate(DateFormat.getDateInstance().parse("2022-05-31T02:30:32.558Z"));
        policyVo.setPolicyReadCount(0);
        policyVo.setPolicyStarcount(0);
        policyVo.setPolicyText("string");
        policyVo.setPolicyTitle("string");
        policyVo.setUId(0);
        String requestJson = JSONObject.toJSONString(policyVo) ;

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                        .post("http://47.96.28.215:8088/api/policy/add")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                        .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void updatePolicyTest() throws Exception {

        PolicyDto policyVo = new PolicyDto( );
        policyVo.setPolicyId(0);
        policyVo.setPolicyAuther("string");
        policyVo.setPolicyBriefintroduction("string");
        policyVo.setPolicyCollectCount(0);
        policyVo.setPolicyDate(DateFormat.getDateInstance().parse("2022-05-31T04:15:14.319Z"));
        policyVo.setPolicyReadCount(0);
        policyVo.setPolicyStarcount(0);
        policyVo.setPolicyText("string");
        policyVo.setPolicyTitle("string");
        policyVo.setUId(0);

        String requestJson = JSONObject.toJSONString(policyVo) ;

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                .post("http://47.96.28.215:8088/api/policy/update")
                .accept(MediaType.APPLICATION_JSON)
                .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void getPolicyTest() throws Exception {
        PolicyVo policyVo = new PolicyVo( ) ;
        policyVo.setPolicyId(2);
        String requestJson = JSONObject.toJSONString(policyVo) ;

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                .post("http://47.96.28.215:8088/api/policy/delete")
                .accept(MediaType.APPLICATION_JSON)
                .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void deletePolicyTest() throws Exception {
        PolicyVo policyVo = new PolicyVo( ) ;
        policyVo.setPolicyId(103);
        String requestJson = JSONObject.toJSONString(policyVo) ;

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                .post("http://47.96.28.215:8088/api/policy/delete")
                .accept(MediaType.APPLICATION_JSON)
                .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void selectPolicyListTest() throws Exception {

        PolicySelectDto policySelectDto = new PolicySelectDto( );
        policySelectDto.setPolicyAuther("string");
        policySelectDto.setPolicyStartDate(DateFormat.getDateInstance().parse("2022-05-31T04:15:40.023Z"));
        policySelectDto.setPolicyEndDate(DateFormat.getDateInstance().parse("2022-05-31T04:15:40.023Z"));
        policySelectDto.setPolicyTitle("string");

        String requestJson = JSONObject.toJSONString(policySelectDto) ;

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                .post("http://47.96.28.215:8088/api/policy/page")
                .accept(MediaType.APPLICATION_JSON)
                .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void selectPolicyPageTest() throws Exception {

        PolicySelectDto policySelectDto = new PolicySelectDto();
        policySelectDto.setPolicyAuther("string");
        policySelectDto.setPolicyStartDate(DateFormat.getDateInstance().parse("2022-05-31T04:15:40.023Z"));
        policySelectDto.setPolicyEndDate(DateFormat.getDateInstance().parse("2022-05-31T04:15:40.023Z"));
        policySelectDto.setPolicyTitle("string");

        String requestJson = JSONObject.toJSONString(policySelectDto);

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders
                .post("http://47.96.28.215:8088/api/policy/pageList/1/0")
                .accept(MediaType.APPLICATION_JSON)
                .contentType (MediaType.APPLICATION_JSON).content(requestJson.getBytes())
                .accept(MediaType.APPLICATION_JSON)
        );
        resultActions.andReturn().getResponse().setCharacterEncoding("UTF-8");
        resultActions.andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

}
